package com.platform.usercenter.utils;

import io.swagger.models.auth.In;
import org.apache.poi.ss.formula.functions.Na;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class GenerateUtil {

    /**
     * 按照日期选择范围及周期生成日期集合
     * @param startDate
     * @param endDate
     * @param indexDay
     * @return
     * @throws ParseException
     */
    public static List<Date> getDateList(String startDate,String endDate,Integer indexDay) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Date start = sdf.parse(startDate);
        Date end = sdf.parse(endDate);
        //获取起始日期
        calendar.setTime(start);
        //获取距离目标日最近的起始日期
        Date begin = getNearListDateAfter(indexDay, calendar,end);
        //获取结束日期
        calendar.setTime(end);
        //获取距离目标日最近的结束日期
        Date off = getNearListDateBefore(indexDay, calendar,start);
        return generateDateList(begin,off);
    }

    public static List<Date> generateDateList(Date begin,Date off){
        List<Date> dateList = new ArrayList<>();
        if (begin == null || off == null){
            return null;
        }else if (begin.compareTo(off) == 0){
            dateList.add(begin);
            return dateList;
        }else if (begin.after(off)){
            return null;
        }else {
            Calendar c1 = Calendar.getInstance();
            c1.setTime(begin);
            Calendar c2 = Calendar.getInstance();
            c2.setTime(off);
            for (int i = 0; ; i++) {
                //获取七天后的日期
                c1.add(Calendar.WEEK_OF_YEAR,1);
                if (c1.before(c2)){
                    dateList.add(c1.getTime());
                }else {
                    dateList.add(0,begin);
                    dateList.add(off);
                    return dateList;
                }
            }
        }
    }

    /**
     * 获取离起始日期最近的日期
     * @return
     */
    public static Date getNearListDateAfter(Integer indexDay,Calendar calendar,Date end){
        for (int i = 0; ; i++) {
            int num = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            if (num == indexDay){
                Date targetDate = calendar.getTime();
                if ((targetDate.compareTo(end) == 0)||targetDate.before(end)){
                    return targetDate;
                }else {
                    return null;
                }
            }else {
                calendar.add(Calendar.DAY_OF_MONTH,1);
            }
        }
    }

    /**
     * 获取离结束日期最近的日期
     * @return
     */
    public static Date getNearListDateBefore(Integer indexDay,Calendar calendar,Date start){
        for (int i = 0; ; i++) {
            int num = calendar.get(Calendar.DAY_OF_WEEK) - 1;
            if (num == indexDay){
                Date targetDate = calendar.getTime();
                if ((targetDate.compareTo(start) == 0)||targetDate.after(start)){
                    return targetDate;
                }else {
                    return null;
                }
            }else {
                calendar.add(Calendar.DAY_OF_MONTH,-1);
            }
        }
    }

    /**
     * 判断当前日期是一周中的第几天
     * @param targetDate
     * @return
     */
    public static Integer getCountOfWeek(Date targetDate){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(targetDate);
        return calendar.get(Calendar.DAY_OF_WEEK) - 1;
    }

    public static void main(String[] args) throws ParseException {
        /**
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date parse = sdf.parse("2023-10-04");
        calendar.setTime(parse);
        int num1 = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        System.out.println("num:"+num1);
        calendar.add(Calendar.WEEK_OF_YEAR,1);
        System.out.println(sdf.format(calendar.getTime()));
        int num2 = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        System.out.println("num:"+num2);
        Calendar c1 = Calendar.getInstance();
        c1.setTime(parse);
        Calendar c2 = Calendar.getInstance();
        c2.setTime(parse);
        System.out.println("比较："+c1.before(c2));
         */
        /**
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date d1 = sdf.parse("2023-10-04");
        Date d2 = sdf.parse("2023-10-05");
        System.out.println(d1.after(d2));
         */
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date begin = sdf.parse("2023-10-04");
        Date end = sdf.parse("2023-10-04");
        List<Date> dates = getDateList("2023-10-04", "2023-12-07",1);
        if (dates != null){
            dates.stream().forEach(d -> System.out.println(sdf.format(d)));
        }else {
            System.out.println("日期范围选择异常");
        }
    }
}
